﻿using Furion;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TcpClient.Core.SqlSugar
{
    public static class SqlsugarSetup
    {
        public static  void AddSqlsugarSetup(this IServiceCollection services)
        {
            var dbtype = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
            //如果多个数数据库传 List<ConnectionConfig>
            var configConnection = new ConnectionConfig()
            {
                DbType = dbtype[0].DbType,
                ConnectionString = dbtype[0].ConnectionString,
                IsAutoCloseConnection = dbtype[0].IsAutoCloseConnection,
            };

            SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        //Console.WriteLine(sql);//输出sql
                    };
                });

            services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
        }
    }
}
